package com.rong.DAO;

import com.opensymphony.xwork2.ActionContext;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.rong.ORM.ZynBusiInput;
import com.rong.ORM.ZynCompany;
import com.rong.ORM.ZynQueryCriter;
import com.rong.ORM.ZynSell;
import com.rong.ORM.ZynUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZynCompany entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynCompany
 * @author MyEclipse Persistence Tools
 */

public class ZynCompanyDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZynCompanyDAO.class);
	// property constants
	public static final String COM_NAME = "comName";
	public static final String COM_PROPERTY = "comProperty";
	public static final String COM_AB = "comAb";
	public static final String COM_DISTRICT = "comDistrict";
	public static final String COM_TYPE = "comType";
	public static final String COM_ADDRESS = "comAddress";
	public static final String COM_CON_PERSON1 = "comConPerson1";
	public static final String COM_CON_PERSON2 = "comConPerson2";
	public static final String EXT_ATTR1 = "extAttr1";
	public static final String EXT_ATTR2 = "extAttr2";
	public static final String EXT_ATTR3 = "extAttr3";
	public static final String EXT_ATTR4 = "extAttr4";
	public static final String EXT_ATTR5 = "extAttr5";

	public void save(ZynCompany transientInstance) {
		log.debug("saving ZynCompany instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().save(transientInstance);
			ts.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynCompany persistentInstance) {
		log.debug("deleting ZynCompany instance");
		try {
			Transaction ts = null;
            ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynCompany findById(java.lang.Integer id) {
		log.debug("getting ZynCompany instance with id: " + id);
		try {
			ZynCompany instance = (ZynCompany) getSession().load(ZynCompany.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynCompany instance) {
		log.debug("finding ZynCompany instance by example");
		try {
			List results = getSession().createCriteria(
					"com.rong.DAO.ZynCompany").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynCompany instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZynCompany as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByComName(Object comName) {
		return findByProperty(COM_NAME, comName);
	}

	public List findByComProperty(Object comProperty) {
		return findByProperty(COM_PROPERTY, comProperty);
	}

	public List findByComAb(Object comAb) {
		return findByProperty(COM_AB, comAb);
	}

	public List findByComDistrict(Object comDistrict) {
		return findByProperty(COM_DISTRICT, comDistrict);
	}

	public List findByComType(Object comType) {
		return findByProperty(COM_TYPE, comType);
	}

	public List findByComAddress(Object comAddress) {
		return findByProperty(COM_ADDRESS, comAddress);
	}

	public List findByComConPerson1(Object comConPerson1) {
		return findByProperty(COM_CON_PERSON1, comConPerson1);
	}

	public List findByComConPerson2(Object comConPerson2) {
		return findByProperty(COM_CON_PERSON2, comConPerson2);
	}

	public List findByExtAttr1(Object extAttr1) {
		return findByProperty(EXT_ATTR1, extAttr1);
	}

	public List findByExtAttr2(Object extAttr2) {
		return findByProperty(EXT_ATTR2, extAttr2);
	}

	public List findByExtAttr3(Object extAttr3) {
		return findByProperty(EXT_ATTR3, extAttr3);
	}

	public List findByExtAttr4(Object extAttr4) {
		return findByProperty(EXT_ATTR4, extAttr4);
	}

	public List findByExtAttr5(Object extAttr5) {
		return findByProperty(EXT_ATTR5, extAttr5);
	}

	public List findAll() {
		log.debug("finding all ZynCompany instances");
		try {
			String queryString = "from ZynCompany";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynCompany merge(ZynCompany detachedInstance) {
		log.debug("merging ZynCompany instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			ZynCompany result = (ZynCompany) getSession().merge(detachedInstance);
			ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynCompany instance) {
		log.debug("attaching dirty ZynCompany instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynCompany instance) {
		log.debug("attaching clean ZynCompany instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List<ZynCompany> findProptAndBlto(String property,String blongToCom) {
				
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();		
		Criteria crt = getSession().createCriteria(ZynCompany.class);				
			crt.add(Restrictions.eq("comProperty", property));
			crt.add(Restrictions.eq("extAttr3", blongToCom));
		ts2.commit();
		List<ZynCompany> list = crt.list();
	
		return list;
	}
	
	
	
	/**
	 * 查找并返回符合条件的公司
	 */

	public List<ZynCompany> findComQueryByPage(int page, int rowsPerPage,String comname,int blongto,String distric,String ComProperty) {
		
		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();
				
		Criteria crt = getSession().createCriteria(ZynCompany.class);
		crt.addOrder(org.hibernate.criterion.Order.desc("id"));
		
		if(comname!=null&&!comname.equalsIgnoreCase("")){			//公司名称模糊匹配
			crt.add(Restrictions.like("comName", comname,MatchMode.ANYWHERE));
		}
	
		if(distric!=null&&!distric.equalsIgnoreCase("")){			//公司地区模糊匹配
			crt.add(Restrictions.like("comDistrict", distric,MatchMode.ANYWHERE));
		}
		if(ComProperty!=null&&!ComProperty.equalsIgnoreCase("")){			//公司性质
			crt.add(Restrictions.eq("comProperty", ComProperty));
		}
	
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
			//总部
			if(blongto>-1){			//所属分公司
				crt.add(Restrictions.eq("extAttr3",String.valueOf(blongto)));
			}
		}else{			
					//所属分公司
				crt.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
			  //匹配单据的所属公司
		}
		
		
		crt.setMaxResults(rowsPerPage); // 每页最多显示几条
		crt.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
		ts2.commit();
		List<ZynCompany> list = crt.list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("==========comlist by query======"+list.get(i).getComName());
			
		}
        
		

		return list;
	}
	/**
	 * 查找并返回所有公司数
	 */

	public List<ZynCompany> comTatolRows(String comname,int blongto,String distric,String ComProperty) {
		getSession().clear();

		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	     Criteria crt = getSession().createCriteria(ZynCompany.class);
			crt.addOrder(org.hibernate.criterion.Order.desc("id"));
			
			if(comname!=null&&!comname.equalsIgnoreCase("")){			//公司名称模糊匹配
				crt.add(Restrictions.like("comName", comname,MatchMode.ANYWHERE));
			}
		
			if(distric!=null&&!distric.equalsIgnoreCase("")){			//公司地区模糊匹配
				crt.add(Restrictions.like("comDistrict", distric,MatchMode.ANYWHERE));
			}
			if(ComProperty!=null&&!ComProperty.equalsIgnoreCase("")){			//公司性质
				crt.add(Restrictions.eq("comProperty", ComProperty));
			}
			//--权限判断,分公司只能看本公司单据，本部可以查看全部		
			if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
				//总部
				if(blongto>-1){			//所属分公司
					crt.add(Restrictions.eq("extAttr3", String.valueOf(blongto)));
				}
			}else{			
						//所属分公司
					crt.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
				  //匹配单据的所属公司
			}
			
			
		List<ZynCompany> list = crt.list();
		return list;
	}

	
}